﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CrystalDecisions.CrystalReports.Engine;

public partial class Admin_DoanhThuReport : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnCHon_Click(object sender, EventArgs e)
    {
        
    }
    protected void btnCHon_Click1(object sender, EventArgs e)
    {
        string thang = DropDownList1.SelectedItem.Text.ToString();
        string nam = DropDownList2.SelectedItem.Text.ToString();
        string cuoithang = "28";
        if (thang == "1" || thang == "3" || thang == "5" || thang == "7" || thang == "8" || thang == "10" || thang == "12")
            cuoithang = "31";
        else if (thang == "4" || thang == "6" || thang == "9" || thang == "11")
            cuoithang = "30";
        else if ((Int32.Parse(nam) % 4) == 0)
            cuoithang = "29";
        else cuoithang = "28";
        //string trangthai = "Đã thanh toán";
        string sql = string.Format(@"
                                    SELECT
                                        a.MaXe ,
                                        b.TenXe ,
                                        c.GiaBan,
                                        SUM(SoLuongMua) as SoLuongMua,
                                        c.GiaBan*SUM(SoLuongMua) as ThanhTien
                                    FROM OrderDetail a INNER JOIN ThongTinCoBan b 
                                                 ON a.MaXe = b.MaXe
                                                 INNER JOIN Orders d
                                                 ON a.OrderID=d.OrderID
                                                 INNER JOIN gia c
                                                 ON a.MaXe=c.MaXe
                                    WHERE d.TrangThai IN(N'Đã thanh toán',N'Hoàn thành')  AND d.NgayNhan BETWEEN '{0}/1/{1} 00:00:00' AND '{0}/{2}/{1} 00:00:00'
                                    GROUP BY a.MaXe,b.TenXe,c.GiaBan
                                    ORDER By a.MaXe
                                    ",
                                       thang,
                                       nam,
                                       cuoithang
                                       );
        DataTable dt = DataBase.GetDataTable(sql);
        dsDoanhThu ds = new dsDoanhThu();
        int dong = -1;
        foreach (DataRow dtr in dt.Rows)
        {
            dong++;
            ds.dtDoanhThu.Rows.Add();
            ds.dtDoanhThu.Rows[dong]["MaXe"] = dtr["MaXe"].ToString();
            ds.dtDoanhThu.Rows[dong]["TenXe"] = dtr["TenXe"].ToString();
            ds.dtDoanhThu.Rows[dong]["GiaBan"] = dtr["GiaBan"].ToString();
            ds.dtDoanhThu.Rows[dong]["SoLuong"] = dtr["SoLuongMua"].ToString();
            ds.dtDoanhThu.Rows[dong]["ThanhTien"] = dtr["ThanhTien"].ToString();

        }
        ReportDocument objreport = new ReportDocument();
        objreport.Load(Server.MapPath("~/Admin/BaoCao/rpDoanhThu.rpt"));
        objreport.SetDataSource(ds);
        this.CrystalReportViewer1.ReportSource = objreport;
        CrystalReportViewer1.RefreshReport();
    }
    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("BaoCao.aspx");
    }
}
